package com.net.library.util;

import android.content.Context;
import com.socks.library.KLog;

import java.net.ConnectException;

public class NetErrStringUtils {

    private static final int ERR_404 = 404;

    private static final int ERR_500 = 500;

    private static final int ERR_401 = 401;

    private static final int ERR_502 = 502;

    public static String getErrString(Context context, int code) {
        KLog.d(code+"");
        String result;
        switch (code) {
            case ERR_404:
                result = "无法找到指定位置的资源";
                break;
            case ERR_500:
                result = "内部服务器错误";
                break;
            case ERR_502:
                result = "网关错误，无法连接到服务器";
                break;
            case ERR_401:
                result = "网址错误";
                break;
            case 0:
                result="请查看设备是否联网，若已联网请联系后台服务是否开启";
                break;

            default:
                result = "网络错误";
                break;
        }
        return result;
    }

    public static String getErrString(Context context, String t){
        KLog.d(t);
        if(t==null){
            return "无网络服,请查看设备是否联网,若已联网请联系后台服务是否开启";
        }else if(t.equals("2")){
            return "请先登陆";
        }else if(t.equals("0")){
            return "解析数据出错";
        }else if(t.toLowerCase().contains("timeout")){
            return "链接超时";
        }
        return "无网络服,请查看设备是否联网,若已联网请联系后台服务是否开启";
    }

    public static String getErrString(Context context, Throwable t) {
        String result;
        if(t == null){
            result="连接失败";
        }else if (t instanceof java.net.SocketTimeoutException) {
            result = "连接超时";
        } else if(t instanceof ConnectException){
            result="连接失败";
        }else if (t.getMessage() != null && t.getMessage().equalsIgnoreCase("canceled")) {
            result = "";
        } else {
            KLog.e(t.getMessage());
            result = "网络错误";
        }
        return result;
    }
}
